<?php

namespace App\Http\Controllers\Admin\Framework\Common;

use App\Http\Controllers\Admin\ParentController;
use App\Master\Framework\Extend\DataUtil;
use App\Master\Framework\View\AdminUi;
use App\Models\Admin\Framework\AdminGroupModel;
use App\Models\Admin\Framework\AdminMenusModel;
use App\Models\Admin\Framework\AdminRoleModel;
use App\Utils\AppResult;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class ManageController extends ParentController
{

    public function __construct(array $attributes = [])
    {
        parent::__construct();
    }

    /**
     * 左边菜单栏
     * @return string
     */
    public function leftMenuItems()
    {
        $users = auth('admin')->user();

        $params = Cache::remember('leftMenuItems', 1, function () use ($users) {

            $menus = AdminMenusModel::query()->select(['id', 'name', 'path', 'icon', 'parent_id']);

            if ($users->is_show) {
                if (!$role = AdminRoleModel::query()->select(['menu_ids','menu_half_ids'])->where(['id' => $users->role_id, 'status' => 1])->where('is_delete', 0)->first()) {
                    return [];
                }
                $menu_ids = json_decode($role->menu_ids,true);
                $menu_half_ids = json_decode($role->menu_half_ids,true);
                $role_menus_ids = array_merge($menu_ids,$menu_half_ids);
                if (count($role_menus_ids) <= 0) {
                    return [];
                }
                $menus->whereIn('id', $role_menus_ids);
            }

            $res = $menus->where(['facility' => 1, 'status' => 1])
                ->where('status', 1)
                ->where('is_delete', 0)
                ->orderBy('sort', 'asc')
                ->orderBy('id', 'asc')
                ->get()->toArray();

            return $res ? array_values(DataUtil::recursion($res)) : [];
        });

        return AppResult::response200('success', $params);
    }

    /**
     * * 页面操作按钮
     * create //添加
     * update //修改
     * remove //删除
     * deletion //批量删除
     * export //导入
     * import //导出
     * @param Request $request
     * @return array|string
     */
    public function operationPermissions(Request $request)
    {
        $users    = auth('admin')->user();
        $menu_id  = $request->menu_id;
        $btn_list = AdminMenusModel::query()->where('parent_id', $menu_id)->where('facility', 2);
        if ($users->is_show) {
            if (!$role = AdminRoleModel::query()->select(['menu_ids'])->where(['id' => $users->role_id, 'status' => 1, 'is_delete' => 0])->first()) {
                return [];
            }
            if (!$role_menus_ids = json_decode($role->menu_ids,true)) {
                return [];
            }
            $btn_list->whereIn('id', $role_menus_ids);
        }
        $btn_list = $btn_list->where('status', 1)->where('is_delete', 0)->select(['id', 'name', 'path', 'action', 'icon'])->get();
        $btn      = [];
        foreach ($btn_list as $value) {
            $btn[$value->action] = [
                'name'   => $value->name,
                'path'   => $value->path,
                'action' => $value->action,
                'icon'   => $value->icon,
            ];
        }

        return AppResult::response200('success', $btn);
    }

    /**
     * 菜单树形结构
     * @param Request $request
     * @return string
     */
    public function getMenuTrees(Request $request)
    {
        $menus = AdminMenusModel::query()->where(['status' => 1,'is_delete' => 0])
            ->select(['id as value', 'name as label', 'parent_id']);

        if ($request->facility == 1) {
            $menus->where('facility', 1);
        }
        $users = auth('admin')->user();
        if ($request->selfs == 1 && $users->is_show) {
            if (!$role = AdminRoleModel::query()->select(['menu_ids', 'menu_half_ids'])->where(['id' => $users->role_id, 'status' => 1,'is_delete' => 0])->first()) {
                return AppResult::response201('fail');
            }
            $menu_ids = json_decode($role->menu_ids,true);
            $menu_half_ids = json_decode($role->menu_half_ids,true);
            $role_menus_ids = array_merge($menu_ids,$menu_half_ids);
            if (count($role_menus_ids) <= 0) {
                return AppResult::response201('fail');
            }
            $menus->whereIn('id', $role_menus_ids);
        }
        $res = $menus->orderBy('sort', 'asc')->orderBy('id','asc')->get()->toArray();
        return AppResult::response200('success',array_values(DataUtil::recursion($res, 'value', 'parent_id')));
    }

    /**
     * 获取组织树形结构
     * @param Request $request
     * @return string
     */
    public function getGroupTrees(Request $request)
    {
        $level = intval($request->level ?? 2);
        return AppResult::response200('success',AdminGroupModel::getGroupParentsTree($level));
    }

    /**
     * 获取角色数据
     * @param Request $request
     * @return string
     */
    public function getRoleItems(Request $request)
    {
        $list = AdminRoleModel::getRoleParentsTree();

        if ($request->is_key) {
            $list = DataUtil::arrayColumnKey($list, 'value');
        }

        return AppResult::response200('success',$list);
    }

    /**
     * 获取图标数据
     * @param Request $request
     * @return string
     */
    public function getIcons(Request $request)
    {
        return AppResult::response200('success',AdminUi::icons());
    }
}